<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import useConfigStore from "@/stores/config";
import useUserStore from "@/stores/user";
import { isWeixin } from "@/utils/channel";
import routeInterceptor from "@/utils/interceptor";
import { useLogin } from "@/hooks/useLogin";
import { getToken } from "@/stores/auth";

onLaunch(async (data) => {
    // 路由拦截器
    routeInterceptor();

    const configStore = useConfigStore();
    const userStore = useUserStore();

    // 获取配置
    await configStore.getConfig();

    uni.hideTabBar(); //隐藏tabbar

    // 设置语言
    uni.setLocale("zh-Hans");

    // #ifdef H5
    // 判断是否关闭渠道
    const path = `/${uni.getLaunchOptionsSync().path}`; //页面路径
    if (
        configStore?.loginConfig?.config?.status == 0 &&
        path !== "/pages/close/index"
    ) {
        uni.reLaunch({
            url: "/pages/close/index",
        });
    }
    // #endif

    if (getToken()) {
        // 获取用户信息
        await userStore.getUser();
    } else {
        // 第三方自动登录
        const login = useLogin();

        // #ifdef MP
        login.getCode();
        // #endif

        // #ifdef H5
        if (isWeixin()) {
            data.query.code ? login.oauth(data.query.code) : login.getCode();
        }
        // #endif
    }
});
onShow(() => {
    console.log("App Show");
});
onHide(() => {
    console.log("App Hide");
});
</script>
<style>
	uni-page-head {
		display: none !important;
	}
</style>
